/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aptech.business;

import com.aptech.domain.JPA.Account;
import com.aptech.domain.JPA.Event;
import com.aptech.domain.JPA.EventRequest;
import com.aptech.domain.JPA.Post;
import com.aptech.domain.JPA.SearchEventCondition;
import com.aptech.domain.JPA.Topic;
import com.aptech.domain.model.AccountDO;
import com.aptech.domain.model.EventDO;
import com.aptech.util.AptechUtility;
import com.aptech.util.Converter;
import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.Sort;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
/**
 *
 * @author Admin
 */
public class EventManager extends BaseManager implements IEventManager {
    private static Logger logger = Logger.getLogger(EventManager.class);
    private SearchEventCondition searchEventCondition;

    /**
     * @return the searchEventCondition
     */
    public SearchEventCondition getSearchEventCondition() {
        return searchEventCondition;
    }

    
    public List<Event> getAllEvent() throws Exception {
        List<Event> eventList= null;
        try {
            Search search = new Search(Event.class);
            search.addSort(Sort.desc("eventId"));
            commonDAO.search(search);
            eventList = commonDAO.findAll(Event.class);
        } catch(Exception e)  {
            logger.debug(e);
        }
        return eventList;
    }
    
    public List<EventDO> searchEvent(SearchEventCondition searchEventCondition) throws Exception {
        final String LOCATION = "searchEvent()";
        logger.debug(LOCATION + ":: BEGIN");
        try {
            Search search = new Search(Event.class);
            Event event = searchEventCondition.getEvent();
            if(searchEventCondition.getEvent() != null) {
                if(event.getEventName() != null) {
                    search.addFilterEqual("eventName", event.getEventName());
                }
                if(event.getEventId() != null) {
                    search.addFilterEqual("eventId", event.getEventId());
                }
            }
            List<Event> eventList = commonDAO.searchAndCount(search).getResult();
            List<EventDO> result = new ArrayList<EventDO>();
            for (Event event1 : eventList) {
                result.add(getEvent(event1));
            }
                   
            return result;
        } catch (Exception e) {
            logger.debug(e);  
        }
        return null;
    }

    public EventDO getEventDetails(SearchEventCondition searchEventCondition) throws Exception {
        final String LOCATION = "getEventDetails()";
        logger.debug(LOCATION + ":: BEGIN");
        try {
            if(searchEventCondition != null) {                       
                List<EventDO> eventList = searchEvent(searchEventCondition);
                if(eventList != null) {
                    return eventList.get(0);
                }
            }
        } catch (Exception e) {
            logger.debug(e);  
        }
        return null;
    }

    public void addEvent(EventDO event) {
        final String LOCATION = "addEvent";
        logger.debug(LOCATION + ":: BEGIN");
        try {
            Event eventJPA = new Event();
            BeanUtils.copyProperties(event, eventJPA);
            eventDAO.save(eventJPA);
        } catch (Exception e) {
            logger.debug(e); 
        }
    }
    public void saveEventRequest(int eventId, int accountId) {
        final String LOCATION = "saveEventRequest()";
        logger.debug(LOCATION + ":: BEGIN");
        try {
            Event event = new Event();
            event.setEventId(eventId);
            Account account = new Account();
            account.setAccountId(accountId);

            EventRequest eventRequest = new EventRequest();
            eventRequest.setEvent(event);
            eventRequest.setAccount(account);
            Date today = new Date();
            eventRequest.setCreatedDate(new Timestamp(today.getTime()));
            eventRequest.setStatus("PENDING");
            eventRequestDAO.save(eventRequest);
            
        } catch (Exception e) {
            logger.debug(e);
        }
    }
    public Set<EventRequest> getEventRequestByUsername(String username) {
        List<Account> tmp = accountDAO.findByUsername(username, null);
        Set<EventRequest> tmpEventRequest = null;
        if(tmp != null) {
            Account account = tmp.get(0);
            tmpEventRequest = account.getEventRequests();
        }

        return tmpEventRequest;
    };

    public void saveCommentForEvent(int topicId, String postContent) {
        final String LOCATION = "saveCommentForEvent()";
        logger.debug(LOCATION + ":: BEGIN");
        try {
            Topic topic = new Topic();
            topic.setTopicId(topicId);
            AccountDO account = AptechUtility.getWebUserDetails();
            Post post = new Post();
            Date date = new Date();
            post.setPostTime(new Timestamp(date.getTime()));
            post.setTopic(topic);
            post.setPostContent(postContent);
            
            post.setAccount(Converter.get(account, Account.class));                    
            postDAO.save(post);
        } catch (Exception e) {
            logger.debug(e);
        }
    }

    

}
